package io.renren;

import io.renren.datasources.DynamicDataSourceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@Import({DynamicDataSourceConfig.class})
public class RenrenApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(RenrenApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(RenrenApplication.class);
	}

	/**
	 * 跨域配置（弃用@CrossOrigin，每个controller都要配置，不易管理）
	 * @Author zwj
	 * @Date 2018-08-07
	 *
	corsConfiguration.addAllowedOrigin("http://127.0.0.1:9001");
	corsConfiguration.addAllowedOrigin("http://192.168.1.86:9001");
	corsConfiguration.addAllowedOrigin("http://192.168.1.59:9000");
	corsConfiguration.addAllowedOrigin("http://localhost:63342");

	 */
	private CorsConfiguration buildConfig(){
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		// 1 设置访问源地址

  // corsConfiguration.addAllowedOrigin("http://adv.puyunsoft.com");
    //corsConfiguration.addAllowedOrigin("http://advpuyun.hz.taeapp.com");
    //corsConfiguration.addAllowedOrigin("http://qntrade.puyunsoft.com");//插件pc端域名
    //corsConfiguration.addAllowedOrigin("http://item.puyunsoft.com");//移动端域名
    //corsConfiguration.addAllowedOrigin("http://www.puyunsoft.com");//网站
    corsConfiguration.addAllowedOrigin("http://127.0.0.1:9000");
		corsConfiguration.addAllowedOrigin("http://127.0.0.1:9001");

//
//    corsConfiguration.addAllowedOrigin("https://adv.puyunsoft.com");
//    corsConfiguration.addAllowedOrigin("https://advpuyun.hz.taeapp.com");
//    corsConfiguration.addAllowedOrigin("https://qntrade.puyunsoft.com");//插件pc端域名
//    corsConfiguration.addAllowedOrigin("https://ssltrade.puyunsoft.com");//插件pc端域名-https
//    corsConfiguration.addAllowedOrigin("https://jytest.puyunsoft.com");//插件pc端域名-https
		// 2 设置访问源请求头
		corsConfiguration.addAllowedHeader("*");
		// 3 设置访问源请求方法
		corsConfiguration.addAllowedMethod("*");
		return corsConfiguration;
	}
	@Bean
	public CorsFilter corsFilter(){
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		//4 对接口配置跨域设置  “*”代表全部。”**”代表适配所有接口。
		source.registerCorsConfiguration("/**",buildConfig());
		return new CorsFilter(source);
	}


}
